<?php

declare(strict_types=1);

namespace app\controller\admin;

use app\controller\admin\AdminBaseController;
use think\Request;
use app\model\admin\SysMemberMenu as SysMemberMenuModel;
use think\facade\Db;

class SysMemberMenu extends AdminBaseController
{
    public function index(Request $request)
    {
        $field = "*";
        $data = SysMemberMenuModel::scope('where', $request->param())
            ->scope('dataFilter')
            ->field($field)
            ->order('order_num', 'asc')
            ->select();

        return success($data);
    }

    public function show(Request $request)
    {
        $id = $request->param('id');
        $data = SysMemberMenuModel::where('id', $id)->scope('dataFilter')->find();

        if (!$data) {
            return error('数据不存在');
        }
        return success($data);
    }

    public function save(Request $request)
    {
        $param = $request->param();
        if (isset($param['id'])) {
            unset($param['id']);
        }

        if (isset($param['perms'])) {
            $param['perms'] = str_replace('/', ':', $param['perms']);
        }
        $data = SysMemberMenuModel::create($param);

        if ($data->id) {
            return success($data);
        }
        return error('添加失败');
    }

    public function update(Request $request)
    {
        $id = $request->param('id');
        if (!$id) {
            return error('缺少必要参数');
        }

        $model = SysMemberMenuModel::where('id', $id)->scope('dataFilter')->find();

        $data = $request->only(['menu_name', 'parent_id', 'order_num', 'path', 'component', 'query', 'is_frame', 'is_cache', 'menu_type', 'visible', 'status', 'perms', 'icon', 'remark']);
        if ($data['perms']) {
            $data['perms'] = str_replace('/', ':', $data['perms']);
        }
        $ret = $model->save($data);
        if (!$ret) {
            return error('更新失败');
        }
        return success();
    }

    public function delete(Request $request)
    {
        $id = $request->param('id', '');
        if (!$id) {
            return error('缺少必要参数');
        }

        $ids = explode(',', $id);

        $has_child = SysMemberMenuModel::whereIn('parent_id', $ids)->find();
        if ($has_child) {
            return error("请先删除子菜单");
        }

        Db::startTrans();
        $msg = 'success';
        try {
            $ret = SysMemberMenuModel::destroy($ids);
            DB::table('sys_role_menu')->whereIn('menu_id', $ids)->delete();
            $result = true;
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            $result = false;
            $msg = $e->getMessage();
            // 回滚事务
            Db::rollback();
        }
        return $result ? success([], "删除成功") : error($msg);
    }
    // 查询菜单下拉树结构
    public function treeSelect()
    {
        $data = SysMemberMenuModel::where('status', '1')->field('id,menu_name,parent_id,order_num')->select();
        $treeData = makeTree($data->toArray(), 'id', 'parent_id');
        return success($treeData);
    }

    // 根据角色ID查询菜单下拉树结构
    public function roleMenuTreeselect(Request $request)
    {
        $role_id = $request->param('role_id', 0);
        $data = SysMemberMenuModel::where('status', '1')->field('id,menu_name,parent_id,order_num')->select();
        $menus = makeTree($data->toArray(), 'id', 'parent_id');
        $checkedKeys = Db::table('sys_role_menu')->where('role_id', $role_id)->column('menu_id');
        return success([
            'menus' => $menus,
            'checkedKeys' => $checkedKeys
        ]);
    }
}
